/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.impl.DaftarKelasDAOImpl;
import com.ivanbiz.model.DaftarKelas;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.model.Pengguna;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;
import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Iframe;

/**
 *
 * @author IW20149X
 */
public class DaftarKelasLaporanViewService extends SelectorComposer<Component> {

    @Wire
    Iframe ipdf;
    DaftarKelasDAO daftarKelasDAO;
    SimpleDateFormat simpleDateFormat;
    SimpleDateFormat simpleDateFormatFrom;
    Pengguna pengguna;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        pengguna = (Pengguna) Sessions.getCurrent().getAttribute("pengguna");
        daftarKelasDAO = new DaftarKelasDAOImpl();
        simpleDateFormat = new SimpleDateFormat("dd MMMMM yyyy");
        Kelas kelas = (Kelas) Executions.getCurrent().getArg().get("kelas");
        List<DaftarKelas> listDaftarKelas = daftarKelasDAO.getDataByEqualsOrderByBankAndNama(kelas.getTransactionReference());
        Map map = new HashMap();
        map.put("logo", Executions.getCurrent().getDesktop().getWebApp().getRealPath("/img/logo.jpg"));
        map.put("kelas.tempatKelas", kelas.getTempatKelas() + " , " + simpleDateFormat.format(kelas.getTanggalKelas()));
        map.put("kelas.tanggalFrom", kelas.getTanggalKelas());
        String reportFile = null;
        if (kelas.getTanggalKelas2() == null) {
            reportFile = Executions.getCurrent().getDesktop().getWebApp().getRealPath("/report/DaftarKelasReport.jasper");
        } else {
            map.put("kelas.tanggalTo", kelas.getTanggalKelas2());
            reportFile = Executions.getCurrent().getDesktop().getWebApp().getRealPath("/report/DaftarKelasReport2.jasper");
        }

        JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile, map, new JRBeanCollectionDataSource(listDaftarKelas));
        loadToStream1(jasperPrint);
//        JasperViewer.viewReport(jasperPrint);
    }

    private void loadToStream(JasperPrint jasperPrint) throws FileNotFoundException, JRException, IOException {
        File f = new File(Executions.getCurrent().getDesktop().getWebApp().getRealPath("/report/daftarKelasReport.pdf"));
        OutputStream output = new FileOutputStream(f.getAbsoluteFile());
        JasperExportManager.exportReportToPdfStream(jasperPrint, output);
        output.flush();
        output.close();
        byte[] buffer = new byte[(int) f.length()];
        FileInputStream fs = new FileInputStream(f);
        fs.read(buffer);
        fs.close();
        ByteArrayInputStream is = new ByteArrayInputStream(buffer);
        AMedia aMedia = new AMedia(f.getName(), "pdf", "application/pdf", is);
        ipdf.setContent(aMedia);
        f.delete();
    }

    private void loadToStream1(JasperPrint jasperPrint) throws JRException, IOException {
        ByteArrayOutputStream bytesOutputStream = new ByteArrayOutputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(bytesOutputStream);

        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, bufferedOutputStream);
        exporter.setParameter(JRPdfExporterParameter.USER_PASSWORD, pengguna.getUserName());
        exporter.setParameter(JRPdfExporterParameter.OWNER_PASSWORD, pengguna.getPassword());
        exporter.setParameter(JRPdfExporterParameter.IS_128_BIT_KEY, Boolean.TRUE);
        exporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED, Boolean.TRUE);
//        exporter.setParameter(JRPdfExporterParameter.PERMISSIONS, PdfWriter.ALLOW_COPY);
        exporter.exportReport();

        InputStream mediais = new ByteArrayInputStream(bytesOutputStream.toByteArray());
        bytesOutputStream.close();

        AMedia amedia = new AMedia(jasperPrint.getName() + ".pdf", "pdf", "application/pdf", mediais);
        ipdf.setContent(amedia);
    }
}
